package com.gis.controller;

import java.util.List;

import com.gis.entity.AvgStationData;
import com.gis.entity.FloatPlantData;
import com.gis.exception.CustomException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gis.entity.OceanBouyData;
import com.gis.result.ResponseResult;
import com.gis.service.OceanBouyDataService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestParam;


@RestController
@RequiredArgsConstructor
@RequestMapping("/ocean_bouy_data")
@Tag(name = "海洋浮标", description = "海洋浮标数据接口")
@CrossOrigin(origins = "*")
public class OceanBouyDataController {
    @Autowired
    private OceanBouyDataService oceanBouyDataService;

    @Operation(summary = "获取所有海洋浮标数据")
    @GetMapping("/all")
    public ResponseResult<List<OceanBouyData>> GetAllOceanBouyData() {
        List<OceanBouyData> sedimentDatas = oceanBouyDataService.list();
        return ResponseResult.success(sedimentDatas);
    }
    
    @Operation(summary = "根据站点ID获取海洋浮标数据")
    @Parameters({
        @Parameter(name = "stationId", description = "站点ID", required = true)
    })
    @GetMapping("getByStationId")
    public ResponseResult<List<OceanBouyData>> getByStationId(@RequestParam Integer stationId) {
        QueryWrapper<OceanBouyData> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("station_id", stationId);
        List<OceanBouyData> oceanBouyDatas = oceanBouyDataService.list(queryWrapper);
        return ResponseResult.success(oceanBouyDatas);
    }


    @Operation(summary = "获取某时间段内数据")
    @GetMapping("getFromYearAndMonth")
    public ResponseResult<List<OceanBouyData>> getFromYearAndMonth(int stationId, int fromYear, int fromMonth, int toYear, int toMonth) {
        List<OceanBouyData> oceanBouyDataList = oceanBouyDataService.getFromYearAndMonth(stationId, fromYear, fromMonth, toYear, toMonth);
        if(oceanBouyDataList.isEmpty()){
            throw new CustomException("无数据");
        }
        return ResponseResult.success(oceanBouyDataList);
    }

    @Operation(summary = "更新海洋浮标数据")
    @PostMapping("/update")
    @Parameters({
        @Parameter(name = "oceanBouyData", description = "海洋浮标数据", required = true)
    })
    public ResponseResult<OceanBouyData> UpdateOceanBouyData(@RequestBody OceanBouyData oceanBouyData) {
        oceanBouyDataService.saveOrUpdate(oceanBouyData);
        return ResponseResult.success(oceanBouyData);
    }

    @Operation(summary = "根据ID删除海洋浮标数据")
    @PostMapping("/deleteById")
    @Parameters({
        @Parameter(name = "id", description = "海洋浮标数据ID", required = true)
    })
    public ResponseResult<String> DeleteOceanBouyDataById(@RequestBody Object json) {
        JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(json));
        Integer id = jsonObject.getInteger("id");
        oceanBouyDataService.removeById(id);
        return ResponseResult.success("delete successfully");
    }
    
    @Operation(summary = "分页获取海洋浮标数据")
    @Parameters({
        @Parameter(name = "pageNo", description = "页码", required = true),
        @Parameter(name = "pagesize", description = "每页数量", required = true)
    })
    @GetMapping("/listPage")
    public ResponseResult<IPage<OceanBouyData>> ListPage(@RequestParam Integer pageNo, @RequestParam Integer pageSize) {
        IPage<OceanBouyData> page = new Page<>(pageNo, pageSize, true);
        return ResponseResult.success(oceanBouyDataService.page(page));
    }
    
}
